var table;
var $;
var layer;
var form;
layui.use(['table', 'form', 'layer', 'jquery'], function () {
    table = layui.table;
    layer = parent.layui.layer;
    form = layui.form;
    $ = layui.jquery;

    var index = parent.layer.load(1, {
        shade: [0.3, '#000']
    });

    top.userTable = table.render({
        elem: '#user_table'
        , limit: 5
        , limits: [5]
        , page: true
        , where: {
            /*
            appName:appName,
            state:state
            */
        }
        , url: '/admin/listUsers' //数据接口
        , page: true //开启分页
        , cols: [[ //表头
            {field: 'id', title: 'ID', align: 'center', sort: true, width: 180}
            , {field: 'userName', title: '用户名', align: 'center'}
            , {field: 'accessKey', title: '账号Key', align: 'center', width: 320}
            //  ,{field: 'accessSecret', title: '账号Secret', align: 'center'}
            , {
                field: 'status', title: '状态', align: 'center', sort: true, width: 100, templet: function (el) {
                    if (el.status == 0) {
                        return '<input id="status" type="checkbox" lay-filter="status" name="' + el.userName + '" lay-skin="switch" lay-text="正常|禁用" onclick="changeStatus()">';
                    } else {
                        return '<input id="status" type="checkbox" name="' + el.userName + '" lay-filter="status"  lay-skin="switch" lay-text="正常|禁用" onclick="changeStatus()" checked>';
                    }
                }
            }
            , {field: 'createTime', title: '创建时间', align: 'center', sort: true}
            , {
                field: 'createDateStr', title: '操作', align: 'center', width: 80, templet: function (el) {
                    var user = JSON.stringify(el);
                    return '<a class="layui-btn layui-btn-sm layui-btn-radius" ' +
                        "onclick=info(" + el.id + ",\"" + el.userName + "\",\"" + el.accessKey + "\",\"" + el.accessSecret + "\",\"" + el.signToken + "\",\"" + el.createTime + "\",\"" + el.modifyTime + "\",\"" + el.status + "\") title='查看详情'>详&nbsp;情</a>";
                }
            }
        ]]
        , done: function (res, curr, count) {
            parent.layer.close(index);
        }
    });

    //监听开关
    form.on('switch(status)', function (data) {
        var index = parent.layer.load(1, {
            shade: [0.3, '#000']
        });
        var status = this.checked ? 1 : 0
        var status_switch = $(data.elem);
        var userName = status_switch.attr("name");
        $.ajax({
            url: '/admin/refresh',
            dataType: 'json',
            data: {
                "userName": userName,
                "status": status
            },
            success: function (res) {
                if (res.code == 0) {
                    var successMsg = status == 1 ? "已<span style='color:#61BFFF'>启用</span>该账户" : "已<span style='color:red'>禁用</span>该账户"
                    layer.msg(successMsg, {icon: 1});
                } else {
                    status_switch.attr("checked", true);
                    form.render('switch', status);
                    layer.msg(res.msg, {icon: 2});
                }
            }, complete: function (res) {
                parent.layer.close(index);
            }
        });
    });

});

function condition(){
    var userName = $('#searchUserName').val();
    if(userName.trim().length==0){
        userName = null;
    }
    var status = $('#select_status').val();
    if(status.trim().length==0){
        status = null;
    }
    return {
        "userName":userName
        ,"status":status
    }
}

function info(userId, userName, accessKey, accessSecret, signToken, createTime, modifyTime, status) {
    top.userId = userId;
    layer.open({
        title: '用户详情',
        type: 2,
        area: ['960px', '640px'],
        content: '/admin/userInfo.html?userId=' + userId,
        success: function (layero, index) {
            var body = layer.getChildFrame('body', index);
            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            body.find('#userId').val(userId);
            body.find('#userName').val(userName);
            body.find('#accessKey').val(accessKey);
            body.find('#accessSecret').val(accessSecret);
            body.find('#signToken').val(signToken);
            body.find('#createTime').val(createTime);
            body.find('#modifyTime').val(modifyTime);
            body.find('#status').val(status == 1 ? "正常" : "已禁用");
        }
    });
}

function query(){
    refreshTable();
}

function refreshTable() {
    var index = parent.layer.load(1, {
        shade: [0.3, '#000']
    });
    table.reload('user_table', {
        url: '/admin/listUsers'
        ,where: condition()
    }, true);
    parent.layer.close(index);
}

function toAddUser() {

    layer.prompt({
        formType: 0,
        value: '',
        title: '输入用户名(其他用户数据由系统生成)',
        area: ['320px', '180px'] //自定义文本域宽高
    }, function (value, index, elem) {
        $.ajax({
            url: '/admin/createUser',
            datatype: 'json',
            data: {
                "userName": value
            },
            success: function (res) {
                if (res.code == 0) {
                    layer.close(index);
                    layer.msg('创建用户成功', {icon: 1});
                    top.userTable.reload();
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, complete: function (res) {

            }
        });
    });
}

